(function($) {
    $.fn.lazyload = function(options) {
        /*合并参数*/
        var settings = {
            threshold: 0,//一般输入正数，提前开始加载
            failurelimit: 0, //最多显示的个数,一定要传
            event: "scroll",
            effect: "show",
            container: window
        };
        if (options) {
            $.extend(settings, options);
        }
        var elements = this;

        /*容器绑定滚动事件*/
        if ("scroll" == settings.event) {
            $(settings.container).bind("scroll", function(event) {
                var counter = 0;
                elements.each(function() {
                    if ($.abovethetop(this, settings) || $.leftofbegin(this, settings)) {} else {
                        /*下和右同时返回false时手动调用事件,也就是元素在容器内时*/
                        if (!$.belowthefold(this, settings) && !$.rightoffold(this, settings)) {
                            $(this).trigger("appear");
                        } else {
                            if (counter++ > settings.failurelimit) {
                                return false;
                            }
                        }
                    }
                });
                /*获取imgload loaded=false的 jq对象*/
                var temp = $.grep(elements, function(element) {
                    return !element.loaded
                });
                elements = $(temp);
            })
        }

        /*遍历延迟加载的所有图片*/
        this.each(function() {
            var self = this;
            /*获取不到属性就把src绑定过来*/
            if (undefined == $(self).attr("data-original")) {
                $(self).attr("data-original", $(self).attr("src"));
            }
            /*默认好多情况满足一种loaded = false 情况1*/
            // if ("scroll" != settings.event || undefined == $(self).attr("src") || settings.placeholder == $(self).attr("src") || ($.abovethetop(self, settings) || $.leftofbegin(self, settings) || $.belowthefold(self, settings) || $.rightoffold(self, settings))) {
            //     if (settings.placeholder) {
            //         $(self).attr("src", settings.placeholder);
            //     } else {
            //         $(self).removeAttr("src");
            //     }
            //     self.loaded = false;
            // } else {
            // /*非正常情况下为loaded = true 情况2*/
            //     self.loaded = true;
            // }
            self.loaded = false;

            /*单个图片绑定自定义事件*/
            $(self).one("appear", function() {
                if (!this.loaded) { //情况1
                    // $("<img />").bind("load", function() {
                    //     $(self).hide().attr("src", $(self).attr("data-original"))[settings.effect](settings.effectspeed);
                    //     self.loaded = true;
                    // }).attr("src", $(self).attr("data-original"));
                    $(this).attr("src", $(self).attr("data-original"));
                    $(this).removeAttr('data-original');
                }
            });

            /*不是滚动事件时触发*/
            if ("scroll" != settings.event) {
                $(self).bind(settings.event, function(event) {
                    if (!self.loaded) {
                        $(self).trigger("appear");
                    }
                })
            }
        });
        /*初始化滚动一次*/
        $(settings.container).trigger(settings.event);
        /*返回所有图片jq对象*/
        return this
    };

    /*工具函数*/
    $.belowthefold = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).height() + $(window).scrollTop();
        } else {
            var fold = $(settings.container).offset().top + $(settings.container).height()
        }
        return fold <= $(element).offset().top - settings.threshold;
    };
    $.rightoffold = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).width() + $(window).scrollLeft();
        } else {
            var fold = $(settings.container).offset().left + $(settings.container).width()
        }
        return fold <= $(element).offset().left - settings.threshold;
    };
    $.abovethetop = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).scrollTop();
        } else {
            var fold = $(settings.container).offset().top;
        }
        return fold >= $(element).offset().top + settings.threshold + $(element).height();
    };
    $.leftofbegin = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).scrollLeft();
        } else {
            var fold = $(settings.container).offset().left;
        }
        return fold >= $(element).offset().left + settings.threshold + $(element).width();
    };
    $.extend($.expr[":"], {
        "below-the-fold": "$.belowthefold(a, {threshold : 0, container: window})",
        "above-the-fold": "!$.belowthefold(a, {threshold : 0, container: window})",
        "right-of-fold": "$.rightoffold(a, {threshold : 0, container: window})",
        "left-of-fold": "!$.rightoffold(a, {threshold : 0, container: window})"
    })
})(jQuery);



$(function() {
    /*init*/
    var wllist = function() {
        var c = $("img.imgload"),
            e = $('#location >li');
        return {
            c: c,
            e: e
        }
    }();

    wllist.c.lazyload({
        'failurelimit':8,
        'threshold':400
    });

    wllist.e.hover(function() {
        var c = $(this).children('.locat-more');
        if (c) {
            var length = c.children('dl').length;
            var width_val;
            if (length >= 7) {
                length = 7;
            }
            width_val = 130 * length;
            c.css('width', width_val + 'px').show();
        }
    }, function() {
        var c = $(this).children('.locat-more');
        if (c) {
            c.hide();
        }
    });

    $('div.content').bind('click.a', function(event) {
        var cur = $(event.target);
        if (cur.is('a') || cur.is('input')) {
            var parent = cur.closest('.filter-price-wrap');
            if (parent) {
                parent.siblings('.filter-price-wrap').children('.filter-price').css({
                    'background': 'none',
                    'height': 30 + 'px',
                    'border': '1px solid #f9f9f9'
                });
            }
            return;
        } else {
            $('div.filter-price').css({
                'background': 'none',
                'height': 30 + 'px',
                'border': '1px solid #f9f9f9'
            });
        }
    });

    var oul = $('#slide20');
    var num = $('#slide20 > li').length;
    var width = 192 * num;
    oul.css('width', width + 'px');
    wl.drag({
        'a': 'dragmove',
        'b': 'slide20',
        'c': width
    });

});

var wllistfn = {
    filter_pf: function(o) {
        var obj = $(o);
        obj.closest('.filter-price').css({
            'background': '#fff',
            'height': 70 + 'px',
            'border': '1px solid #ccc'
        });
    },
    filter_reset: function(o) {
        var obj = $(o);
        var inputs = obj.closest('.filter-priceb').siblings('.filter-pricet').children('input');
        inputs.each(function() {
            $(this).val('');
        });
    }

};